<?php


if(get('repair')==1){
  $this->removeHooksFromCore();
  $this->addHooksToCore();
  Antz::redir($actionUrls['config']);
}















$myConfig = $DB->fetchAll('SELECT * FROM '.cms_db_prefix().'iusers_config');
$accessGroups = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_groups");
$countries = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_countries ORDER BY title ASC");

$conf = array();
foreach($myConfig as $k=>$v){
    $conf[$v['k']] = $v['v'];
};

if(Antz_IntelliForm::submitted('config')){
    $newParams = array();

    if(!isset($_POST['be_insist_fname'])) $_POST['be_insist_fname'] = '0';
    if(!isset($_POST['fe_insist_fname'])) $_POST['fe_insist_fname'] = '0';
    if(!isset($_POST['be_insist_lname'])) $_POST['be_insist_lname'] = '0';
    if(!isset($_POST['fe_insist_lname'])) $_POST['fe_insist_lname'] = '0';
    
    if(!isset($_POST['be_country_required'])) $_POST['be_country_required'] = '0';
    if(!isset($_POST['fe_country_required'])) $_POST['fe_country_required'] = '0';
    if(!isset($_POST['be_city_required'])) $_POST['be_city_required'] = '0';
    if(!isset($_POST['fe_city_required'])) $_POST['fe_city_required'] = '0';

    $rsf = array();
    if(post('rs_business_name')==1) $rsf[] = 'business_name';
    if(post('rs_fname')==1) $rsf[] = 'first_name';
    if(post('rs_lname')==1) $rsf[] = 'last_name';
    if(post('rs_tnc')==1) $rsf[] = 'tnc';
    if(post('rs_country')==1) $rsf[] = 'country';
    if(post('rs_city')==1) $rsf[] = 'city';
    if(post('rs_address')==1) $rsf[] = 'address';
    if(post('rs_dob')==1) $rsf[] = 'dob';
    if(post('rs_sex')==1) $rsf[] = 'sex';
    if(post('rs_subscribe')==1) $rsf[] = 'subscribe';

    $psf = array();
    if(post('ps_business_name')==1) $psf[] = 'business_name';
    if(post('ps_fname')==1) $psf[] = 'first_name';
    if(post('ps_lname')==1) $psf[] = 'last_name';
    if(post('ps_country')==1) $psf[] = 'country';
    if(post('ps_city')==1) $psf[] = 'city';
    if(post('ps_address')==1) $psf[] = 'address';
    if(post('ps_dob')==1) $psf[] = 'dob';
    if(post('ps_sex')==1) $psf[] = 'sex';
    if(post('ps_subscribe')==1) $psf[] = 'subscribe';

    $_POST['register_show_fields'] = implode(',', $rsf);
    $_POST['profile_show_fields'] = implode(',', $psf);


	unset($_POST['rs_fname'], $_POST['rs_lname'], $_POST['rs_tnc'], $_POST['rs_country'],
	$_POST['rs_city'], $_POST['rs_address'], $_POST['rs_dob'],
	$_POST['rs_sex'], $_POST['rs_subscribe'], $_POST['ps_fname'], $_POST['ps_lname'],
	$_POST['ps_country'], $_POST['ps_city'], $_POST['ps_address'],
	$_POST['ps_dob'], $_POST['ps_sex'], $_POST['ps_subscribe']);

    foreach($_POST as $k=>$v){
        if(!array_key_exists($k, $conf)) continue;
        // do not allow to remove all groups from new users
        if($k == 'default_groups_for_self_registration' && trim($v) == ''){
            Antz::addError($this->Lang('at_least_one_group'));
            continue;
        };
        if($k == 'default_groups_for_admin_registration' && trim($v) == ''){
            Antz::addError('');
            continue;
        };
        //$DB->debug(true);
        //$DB->showErrors(true);
        $DB->update(cms_db_prefix().'iusers_config', array('v'=>$v), "k = '{$k}'");
    };
    Antz::addSuccess($this->Lang('config_success'));
}

$myConfig = $DB->fetchAll('SELECT * FROM '.cms_db_prefix().'iusers_config');
$accessGroups = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_groups");
$countries = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_countries ORDER BY title ASC");

$conf = array();
foreach($myConfig as $k=>$v){
    $conf[$v['k']] = $v['v'];
};



$form = Antz::newObject('Antz_Form', 'config');
$form->setAction($actionUrls['config']);
$block = $form->add('full');
$fs = $block->add($this->Lang('config_settings'));

// access groups for self-registering users
$ag = $this->GetConfig('default_groups_for_self_registration');
$ag = $fs->addInput('text', 'default_groups_for_self_registration', 'Self-registered users belong to', $ag);
$ag->addClass('wide');
$sel = Antz::newObject('Antz_Form_Components_Select', array('conf_ag', 'Add/Remove access groups'));
$sel->add('Add / Remove', '0');
foreach($accessGroups as $k=>$v){
    $sel->add($v['title'].' ['.$v['keyname'].']', $v['keyname']);
};
$ag->addJsEvent('onfocus', 'this.blur()');
$ag->addClass('grayed-out');
echo '<style type="text/css">.grayed-out{background-color: #EEE;}</style>';
$sel->addJsEvent('onchange', 'IUsers_Admin.new_addGroup(\''.$ag->getId().'\', this)');
$sel->setWrap(false);
$sel->showLabel(false);
$ag->addAftertext($sel->render());

// access groups for admin-registering users
$ag = $this->GetConfig('default_groups_for_admin_registration');
$ag = $fs->addInput('text', 'default_groups_for_admin_registration', 'Admin-registered users belong to', $ag);
$ag->addClass('wide');
$sel = Antz::newObject('Antz_Form_Components_Select', array('conf_ag', 'Add/Remove access groups'));
$sel->add($this->Lang('add_remove'), '0');
foreach($accessGroups as $k=>$v){
    $sel->add($v['title'].' ['.$v['keyname'].']', $v['keyname']);
};
$ag->addJsEvent('onfocus', 'this.blur()');
$ag->addClass('grayed-out');
echo '<style type="text/css">.grayed-out{background-color: #EEE;}</style>';
$sel->addJsEvent('onchange', 'IUsers_Admin.new_addGroup(\''.$ag->getId().'\', this)');
$sel->setWrap(false);
$sel->showLabel(false);
$ag->addAftertext($sel->render());

// access groups for new pages
$ag = $this->GetConfig('default_groups_for_new_pages');
$ag = $fs->addInput('text', 'default_groups_for_new_pages', 'Default group access for new pages', $ag);
$ag->addClass('wide');
$sel = Antz::newObject('Antz_Form_Components_Select', array('conf_ag', 'Add/Remove access groups'));
$sel->add('Add / Remove', '0');
foreach($accessGroups as $k=>$v){
    $sel->add($v['title'].' ['.$v['keyname'].']', $v['keyname']);
};
$ag->addJsEvent('onfocus', 'this.blur()');
$ag->addClass('grayed-out');
echo '<style type="text/css">.grayed-out{background-color: #EEE;}</style>';
$sel->addJsEvent('onchange', 'IUsers_Admin.new_addGroup(\''.$ag->getId().'\', this)');
$sel->setWrap(false);
$sel->showLabel(false);
$ag->addAftertext($sel->render());

// default country
$ctry = $fs->addSelect('default_country', 'Default country for user registration', $this->GetConfig('default_country'));
foreach($countries as $k=>$v){
	$ctry->add($v['title'], $v['id']);
};

// form fields to show on register page
$fs->addLongTag('div', '<label></label><h2>'.$this->Lang('register_show_fields').'</h2>')->addClass('component');
$chk = $fs->addCheckboxGroup('register_show_fields');
$showFields = explode(',', $conf['register_show_fields']);
$chk->add('rs_business_name', $this->Lang('business_name'), '1', in_array('business_name', $showFields));
$chk->add('rs_fname', $this->Lang('first_name'), '1', in_array('first_name', $showFields));
$chk->add('rs_lname', $this->Lang('last_name'), '1', in_array('last_name', $showFields));
$chk->add('rs_tnc', $this->Lang('tnc'), '1', in_array('tnc', $showFields));
$chk->add('rs_country', $this->Lang('country'), '1', in_array('country', $showFields));
$chk->add('rs_city', $this->Lang('city'), '1', in_array('city', $showFields));
$chk->add('rs_address', $this->Lang('address'), '1', in_array('address', $showFields));
$chk->add('rs_dob', $this->Lang('dob'), '1', in_array('dob', $showFields));
$chk->add('rs_sex', $this->Lang('sex'), '1', in_array('sex', $showFields));
$chk->add('rs_subscribe', $this->Lang('subscribe'), '1', in_array('subscribe', $showFields));

// form fields to show on profile page
$fs->addLongTag('div', '<label></label><h2>'.$this->Lang('profile_show_fields').'</h2>')->addClass('component');
$chk = $fs->addCheckboxGroup('profile_show_fields');
$showFields = explode(',', $conf['profile_show_fields']);
$chk->add('rs_business_name', $this->Lang('business_name'), '1', in_array('business_name', $showFields));
$chk->add('ps_fname', $this->Lang('first_name'), '1', in_array('first_name', $showFields));
$chk->add('ps_lname', $this->Lang('last_name'), '1', in_array('last_name', $showFields));
$chk->add('ps_country', $this->Lang('country'), '1', in_array('country', $showFields));
$chk->add('ps_city', $this->Lang('city'), '1', in_array('city', $showFields));
$chk->add('ps_address', $this->Lang('address'), '1', in_array('address', $showFields));
$chk->add('ps_dob', $this->Lang('dob'), '1', in_array('dob', $showFields));
$chk->add('ps_sex', $this->Lang('sex'), '1', in_array('sex', $showFields));
$chk->add('ps_subscribe', $this->Lang('subscribe'), '1', in_array('subscribe', $showFields));


// which data is required in front end
$fs->addLongTag('div', '<label></label><h2>'.$this->Lang('fe_requirements').'</h2>')->addClass('component');
$chk = $fs->addCheckboxGroup('fe_requirements');
$chk->add('fe_insist_fname', $this->Lang('first_name'), '1', $conf['fe_insist_fname']);
$chk->add('fe_insist_lname', $this->Lang('last_name'), '1', $conf['fe_insist_lname']);
$chk->add('fe_country_required', $this->Lang('country'), '1', $conf['fe_country_required']);
$chk->add('fe_city_required', $this->Lang('city'), '1', $conf['fe_city_required']);



// which data is required in front end
$fs->addLongTag('div', '<label></label><h2>'.$this->Lang('be_requirements').'</h2>')->addClass('component');
$chk = $fs->addCheckboxGroup('be_requirements');
$chk->add('be_insist_fname', $this->Lang('first_name'), '1', $conf['be_insist_fname']);
$chk->add('be_insist_lname', $this->Lang('last_name'), '1', $conf['be_insist_lname']);
$chk->add('be_country_required', $this->Lang('country'), '1', $conf['be_country_required']);
$chk->add('be_city_required', $this->Lang('city'), '1', $conf['be_city_required']);


// add all the other fields
foreach($myConfig as $k=>$row){
    if($row['k']=='default_groups_for_self_registration') continue;
    if($row['k']=='default_groups_for_admin_registration') continue;
    if($row['k']=='default_groups_for_new_pages') continue;
    if($row['k']=='default_country') continue;
    if($row['k']=='register_show_fields') continue;
    if($row['k']=='profile_show_fields') continue;
    if($row['k']=='be_insist_fname') continue;
    if($row['k']=='be_insist_lname') continue;
    if($row['k']=='fe_insist_fname') continue;
    if($row['k']=='fe_insist_lname') continue;
    if($row['k']=='be_country_required') continue;
    if($row['k']=='fe_country_required') continue;
    if($row['k']=='be_city_required') continue;
    if($row['k']=='fe_city_required') continue;
    

    if($row['field_type']=='boolean'){
        $fs->addLongTag('h2', $row['title']);
        $rad = $fs->addRadioGroup($row['k'], $row['v']);
        $rad->add('1', $this->Lang('yes'));
        $rad->add('0', $this->Lang('no'));
        
        /*
        $sel = $fs->addSelect($row['k'], $row['title'], $row['v']);
        $sel->add($this->Lang('Yes'), '1');
        $sel->add($this->Lang('No'), '0');
        $sel->addAftertext('('.$row['field_type'].')');
        */
    }else if($row['field_type']=='page'){
    	global $gCms;
    	$contentOps = $gCms->GetContentOperations();
		//$sel = $contentOps->CreateHierarchyDropdown('', $row['v'], $row['k'], 0);
		//$fs->addLongTag('div', '<label>'.$row['title'].'</label>'.$sel.'('.$row['field_type'].')')->addClass('component');
	   $sel = $fs->addSelect($row['k'], $row['title'], $row['v']);
	   $Antz->addPagesToDropMenu($sel, '-1');
    }else{
        $inpt = $fs->addInput('text', $row['k'], $row['title'], $row['v']);
        $inpt->addAftertext('('.$row['field_type'].')');
        if($row['field_type']=='string') $inpt->addClass('extra-wide');
    }


};



$fs->addInput('submit', 'submitBtn', '&nbsp;', 'Update config');

$SMARTY->assign('configForm', $form->render());











echo $this->ProcessTemplate('admin.config.php');
